home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 40-Webservers / netscape / security.frm.z / security.frm
Encoding:
Text File  |  1997-07-30  |  10.4 KB  |  423 lines

  1. #!/usr/bin/perl5
  2. #
  3. # security.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: security.frm,v 1.5 1997/06/19 22:26:07 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $pid_file = "/usr/ns-home/admserv/pid";
  26. $start_ns_admin = "/usr/ns-home/start-admin > /dev/null 2>&1";
  27. $stop_ns_admin = "/usr/ns-home/stop-admin > /dev/null 2>&1";
  28.  
  29. $title = "Netscape Admin Server Access Control";
  30. $myname = "security.cgi";
  31. $conf_file = "/usr/ns-home/admserv/ns-admin.conf";
  32. $pw_file = "/usr/ns-home/admserv/admpw";
  33. $dummy = "/usr/ns-home/admserv/ns-admin.conf.tmp";
  34.  
  35. $account_type_lc = "account";
  36. $account_type_uc = "Account";
  37.  
  38. $js_generic = 
  39. "which = \"none\";
  40. type = \"account\";
  41. function runSubmit() {
  42.     if(which == \"add\") return runAdd();
  43.     if(which == \"edit\") return runEdit();
  44.     if(which == \"delete\") return runDelete();
  45.     return runDo();
  46. }
  47. function markAdd() { which = \"add\"; }
  48. function markEdit() { which = \"edit\"; }
  49. function markDelete() { which = \"delete\"; }
  50. $js_error_box
  51. $js_meta
  52. $js_ip
  53. $js_hostname
  54. function runEdit()  {
  55.     Ctrl = document.AccountForm.chosen
  56.     none = true;
  57.     for(j = 0; j < Ctrl.length; j++) {
  58.         if (Ctrl.options[j].selected) { none = false; break; }
  59.     }
  60.     if (none) {
  61.         errorBox (Ctrl, \"To edit an existing \" + type + \", \"
  62.             + \"first select an \" + type + \"\\nfrom the list, \"
  63.             + \"then click the edit button.\");
  64.         return (false);
  65.     }
  66.     return (true);
  67. }
  68. function runDelete() {
  69.     Ctrl = document.AccountForm.chosen;
  70.     none = true;
  71.     for(j = 0; j < Ctrl.length; j++) {
  72.         if (Ctrl.options[j].selected) { none = false; break; }
  73.     }
  74.     if (none) {
  75.         errorBox (Ctrl, \"To delete an existing \" + type + \", first \"
  76.             + \"select an \" + type + \"\\nfrom the list, then click \"
  77.             + \"the delete button.\");
  78.         return (false);
  79.     }
  80.     return (true);
  81. }
  82. function runDo() {
  83.     if (!testStarList(document.AccountForm.clients)) return (false);
  84.     return (true);
  85. }
  86. function is_ip(word) {
  87.     ipChars = \"0123456789\.*\";
  88.     for (ii=0; ii<word.length; ii++) {
  89.         c = word.charAt(ii);
  90.                        if (ipChars.indexOf(c, 0) == -1) {
  91.             return (false);
  92.         }
  93.     }
  94.     return (true);
  95. }
  96. function remove_star(word) {
  97.     if (word.indexOf(\"*\", 0) == -1) return (word);
  98.     new_word = \"\";
  99.     for (jj = 0; jj < word.length; jj++) {
  100.         if (word.charAt(jj) == \"*\") 
  101.             new_word = new_word + \"233\";
  102.         else 
  103.             new_word = new_word + word.charAt(jj);
  104.     }
  105.     return (new_word);
  106. }             
  107. function testStarList(Ctrl) {
  108.     my_count = 0;
  109.     ipChars = \"0123456789\.*\";
  110.     whitespace = \" \\n\\r\\t\\f\";  space = true;
  111.     for(start=0, cur=0; cur < Ctrl.value.length; cur++) {
  112.         for(i = 0; i < whitespace.length; i++) {
  113.             c = whitespace.charAt(i);
  114.             if (Ctrl.value.charAt(cur) == c) { // found whitespace
  115.                 if (space == false) { 
  116.                     space = true;  
  117.                     if(cur != start) { 
  118.                         host = Ctrl.value.substring(start,cur); 
  119.                         my_count++; 
  120.                         if (is_ip(host)) {
  121.                             new_host = remove_star(host);
  122.                             if (!testIPaddress(new_host,false)) { 
  123.                                 errorBox (Ctrl, \"Invalid server IP address: \" + host + \"\\nin server address list.\"); 
  124.                                 return (false); 
  125.                             }
  126.                         } else {
  127.                             if (!testHostname(Ctrl, host, \"hostname\", false)) return (false);
  128.                         } 
  129.                     } 
  130.                 }
  131.                 break;
  132.             } else {                           // found character
  133.                 if (space == true) { space = false;  start = cur; }
  134.             }
  135.         }
  136.     }
  137.     if(cur != start && space == false) { 
  138.         host = Ctrl.value.substring(start,cur); 
  139.         my_count++; 
  140.         if (is_ip(host)) {
  141.             new_host = remove_star(host);
  142.             if (!testIPaddress(new_host,false)) { 
  143.                 errorBox (Ctrl, \"Invalid server IP address: \" + host + \"\\nin server address list.\"); 
  144.                 return (false); 
  145.             } 
  146.         } else {
  147.             if (!testHostname(Ctrl, host, \"hostname\", false)) return (false);
  148.         }
  149.     }
  150.     return (true);
  151. }
  152. function runAdd() {
  153.     Ctrl = document.AccountForm.new_account;
  154.     if (Ctrl.value.length == 0) {
  155.         errorBox(Ctrl, \"To add a new $account_type_lc, first enter the new $account_type_lc name.\");
  156.         return (false);
  157.     }
  158.     if (!testMeta(Ctrl, \"account name\")) return (false);    
  159.     return (true);
  160. }";
  161.  
  162. $js_edit = 
  163. "$js_standard
  164. $js_error_box
  165. $js_meta
  166. function checkForm(form)  {
  167.     if (form.uname.value.length == 0) {
  168.         errorBox(form.uname, \"Account name cannot be empty.\");
  169.         return (false);
  170.     }
  171.     if (form.password.value.length == 0) {
  172.         errorBox(form.uname, \"Password cannot be empty.\");
  173.         return (false);
  174.     }
  175.     if (!testMeta(form.uname, \"account name\")) return (false);
  176.     if (!testMeta(form.password, \"password\")) return (false);
  177.     return (true);
  178. }";
  179.  
  180. print "Content-type: text/html\n\n";
  181.  
  182. &get_fields;
  183. &get_accounts;
  184.  
  185. if (%fld) {
  186.     $fld{'chosen'} =~ /([\w.-]+)/; $chosen = $1;
  187.  
  188.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  189.     $help =~ s/cgi$/hlp/;
  190.     exec $help if ($fld{'help'} eq "Help");
  191.  
  192.     if ($fld{'edit'}) {
  193.         &get_edit($chosen);
  194.     } elsif ($fld{'do_edit'}) {
  195.         &generic;
  196.     } elsif ($fld{'add'}) {
  197.         &get_add;
  198.     } elsif ($fld{'delete'}) {
  199.         &generic($chosen);    
  200.     } elsif ($fld{'doit'}) {
  201.         &do_it;
  202.         &get_accounts;
  203.         &generic;
  204.     } elsif ($fld{'doedit'}) {
  205.         &do_edit;
  206.         &get_accounts;
  207.         &generic;
  208.     }
  209. } else { &generic; }
  210.  
  211. sub do_it {
  212.     &do_delete($fld{'deleted'}) if $fld{'deleted'};
  213.     &process_clients;
  214.     &bounce_server;
  215. }
  216.  
  217. sub process_clients {
  218.     @clients = split(/\s+/, $fld{'clients'});
  219.     foreach (@clients) {
  220.         if ($_ =~ /[^0-9\.\*]/) { push(@hosts, $_); }
  221.         else { push(@adds, $_); }
  222.     }
  223.     if ($#hosts > 0) { $hosts = "(" . join('|', @hosts) . ")"; }
  224.     elsif ($#hosts == 0) { $hosts = $hosts[0]; }
  225.     else { $hosts = ""; }
  226.  
  227.     if ($#adds > 0) { $adds = "(" . join('|', @adds) . ")"; }
  228.     elsif ($#adds == 0) { $adds = $adds[0]; }
  229.     else { $adds = ""; }
  230.  
  231.     $hosts_added = 0;
  232.     $adds_added = 0;
  233.     open(IN, "< $conf_file");
  234.     open(OUT, "> $dummy");
  235.     while(<IN>) {
  236.         if ($_ =~ /^Hosts\s/) {
  237.             $hosts_added = 1;
  238.             if ($hosts ne "") { print OUT "Hosts $hosts\n"; }
  239.         } elsif ($_ =~ /^Addresses\s/) {
  240.             $adds_added = 1;
  241.             if ($adds ne "") { print OUT "Addresses $adds\n"; }
  242.         } else { print OUT $_; }
  243.     }
  244.     if ($hosts ne "" && $hosts_added == 0) {
  245.         print OUT "Hosts $hosts\n"; 
  246.     }
  247.     if ($adds ne "" && $adds_added == 0) {
  248.         print OUT "Addresses $adds\n";
  249.     }
  250.  
  251.     close(IN);
  252.     close(OUT);
  253.  
  254.     rename($dummy, $conf_file);
  255.  
  256.     $message .= "  Client list updated.";
  257. }
  258.  
  259. sub bounce_server {
  260.     open(IN, "< $pid_file");
  261.     $pid = <IN>;
  262.     close(IN);
  263.     if ($pid) {
  264.         system($stop_ns_admin);
  265.         system($start_ns_admin);
  266.     }
  267. }
  268.  
  269. sub do_delete {
  270.     open(IN, "< $pw_file");
  271.     open(OUT, "> $dummy");
  272.     while(<IN>) {
  273.         print OUT $_ unless $_[0] eq (split(/:/, $_))[0];
  274.     }
  275.     close(IN);
  276.     close(OUT);
  277.     
  278.     rename($dummy, $pw_file);
  279.     &bounce_server;
  280.  
  281.     $message .= "$account_type_uc deleted.";
  282. }
  283.  
  284. sub do_edit {
  285.     $salt = &mksalt;
  286.     $pswd = crypt($fld{'password'}, $salt);
  287.     $line = $fld{'uname'} . ":" . $pswd;
  288.  
  289.     if ($fld{'old_account'} ne "") {
  290.         open(IN, "< $pw_file");
  291.         open(OUT, "> $dummy");
  292.         while(<IN>) {
  293.             if ($fld{'old_account'} eq (split(/:/, $_))[0]) {
  294.                 print OUT "$line\n";
  295.             } else { print OUT $_; }
  296.         }
  297.         close(IN);
  298.         close(OUT);
  299.  
  300.         rename($dummy, $pw_file);
  301.  
  302.         $message .= "$account_type_uc edited.";
  303.     } else { 
  304.         open(OUT, ">> $pw_file");
  305.         print OUT "$line\n";
  306.         close(OUT);
  307.  
  308.         $message .= "$account_type_uc added.";
  309.     }
  310.     
  311.     &bounce_server;
  312. }
  313.  
  314. sub get_add {
  315.     &get_edit($fld{'new_account'});
  316. }
  317.  
  318. sub get_edit {
  319.     &js_title_block($title, $js_edit);
  320.     if ($fld{'add'}) { &header_block("Add $account_type_uc"); }
  321.     else { &header_block("Edit $account_type_uc"); }
  322.  
  323.     print "<form name=StandardForm action=$myname method=post "
  324.         . "onSubmit=\"return runSubmit()\">\n"
  325.         . "<i>$message</i>\n"
  326.         . "<center><br><table width=400>\n";
  327.  
  328.     if ($fld{'edit'}) {
  329.         print "<input type=hidden name='old_account' value=$_[0]>\n";
  330.     }
  331.  
  332.     print "<tr><th align=left>$account_type_uc name:<td>"
  333.         . &text("uname", $_[0], 20)
  334.         . "</td></tr>\n";
  335.  
  336.     print "<tr><th align=left>$account_type_uc password:<td>"
  337.         . &text("password", $val{'password'}, 20)
  338.         . "</td></tr>\n";
  339.  
  340.     print "</table></center><br>\n";
  341.  
  342.     print &js_buttons('doedit','Ok','onClick="markOK()"','onClick="markOther()"');
  343. }
  344.     
  345. sub get_accounts {
  346.     undef @accounts;
  347.     open(IN, "< $pw_file");
  348.     while(<IN>) {
  349.         push(@accounts, (split(/:/, $_))[0]);
  350.     }
  351.     close(IN);
  352.  
  353.     undef @clients;
  354.     open(IN, "< $conf_file");
  355.     while(<IN>) {
  356.         if ($_ =~ /^Hosts\s+(.*)/) {
  357.             $list = $1;
  358.             if ($list =~ /\((.*)\)/) {
  359.                 push(@clients, split(/\|/, $1));
  360.             } else {
  361.                 push(@clients, $list);
  362.             }
  363.         } elsif ($_ =~ /^Addresses\s+(.*)/) {
  364.             $list = $1;
  365.             if ($list =~ /\((.*)\)/) {
  366.                 push(@clients, split(/\|/, $1));
  367.             } else {
  368.                 push(@clients, $list);
  369.             }
  370.         }
  371.     }
  372.     close(IN);
  373.     $val{'clients'} = join("\n", @clients);
  374. }
  375.  
  376. sub generic {
  377.     &js_title_block($title, $js_generic);
  378.     &header_block($title);
  379.     
  380.     if ($_[0]) { $message .=  qq|Click "Ok" to save changes.|; }
  381.  
  382.     print "<form name=AccountForm action=$myname method=post "
  383.         . "onSubmit=\"return runSubmit()\">\n";
  384.  
  385.     print "<i>$message</i>\n"
  386.         . "<br>\n"
  387.         . "<h3>Allowed users:</h3><center><table width=400>\n"
  388.         . "<tr><td><input type=submit name=\"add\" onClick=\"markAdd()\" \n"
  389.         . "value=\"Add New $account_type_uc\"\n"
  390.         . "</td><td><input name=\"new_account\" value=\"$val{'new_account'}\"\n" 
  391.         . "size=19 onClick=\"markAdd()\"></td></tr>\n";
  392.         
  393.     if ($#accounts >= 0) {
  394.         if ($_[0]) {
  395.             undef @show_accts;
  396.             foreach $arg (@accounts) {
  397.                 push(@show_accts, $arg) if $arg ne $_[0];
  398.             }
  399.             print "<input type=hidden name=deleted value=$_[0]>\n";
  400.         } else { @show_accts = @accounts; }
  401.  
  402.         print "<tr><td>\n"
  403.             . "<input type=submit name=\"edit\" onClick=\"markEdit()\" \n"
  404.             . "value=\"Edit Selected $account_type_uc\"></td><td rowspan=2>";
  405.         print &choice_list(*show_accts, "chosen", 20);
  406.         print "</td/tr>\n"
  407.             . "<tr><td><input type=submit name=\"delete\" onClick=\"markDelete()\" \n"
  408.             . "value=\"Delete Selected $account_type_uc\"></td></tr>\n";
  409.     }
  410.  
  411.     print "</table></center><br>";
  412.  
  413.     print "<h3>Allowed clients:</h3>\n"
  414.         . "<center>\n"
  415.         . "<textarea name='clients' cols=20 rows=4>"
  416.         . $val{'clients'}
  417.         . "</textarea><br><br>\n";
  418.  
  419.     print &buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"');
  420. }
  421.  
  422.         
  423.